package com.livestocksolutions.ui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

import com.livestocksolutions.Bovine;
import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.util.Comparators;
import com.livestocksolutions.util.Table;

import javax.swing.JButton;

/**
 * 
 * @author Matt Lang
 *
 */
public class ProductivePanel extends JPanel 
{
	/**
	 * Default serialization id
	 */
	private static final long serialVersionUID = 1L;
	JPanel displayPanel = new JPanel();
	/**
	 * Constructor.
	 */
	public ProductivePanel() 
	{
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		
		JLabel lblProductivePanel = new JLabel("Productive Panel");
		lblProductivePanel.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel_1.add(lblProductivePanel);
		
		JPanel panel_2 = new JPanel();
		add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3, BorderLayout.NORTH);
		
		JButton btnBestSellers = new JButton("Show Best Sellers");
		panel_3.add(btnBestSellers);
		
		JButton btnWorstSellers = new JButton("Show Worst Sellers");
		panel_3.add(btnWorstSellers);
		
		panel_2.add(displayPanel, BorderLayout.CENTER);
		
		btnBestSellers.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				displayPanel.removeAll();
				displayPanel.add(getBestSellers());
				displayPanel.validate();
			}
		});
		
		btnWorstSellers.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				displayPanel.removeAll();
				displayPanel.add(getWorstSellers());
				displayPanel.validate();
			}
		});
	}
	
	/**
	 * Calculate best Sellers.
	 * @return Table of best sellers
	 */
	public Table getBestSellers()
	{
		Table returnPanel = new Table("Top 30 Best Sellers", true, "Visual Tag", "No. of Offspring", "Offspring sold", 
				"Offspring not yet sold", "Average Sell Price");
		returnPanel.setHeaderClickable(0, false);
		returnPanel.setHeaderClickable(1, false);
		returnPanel.setHeaderClickable(2, false);
		returnPanel.setHeaderClickable(3, false);
		returnPanel.setHeaderClickable(4, false);
		ArrayList<Bovine> bovines = new ArrayList<Bovine>(Arrays.asList(LivestockSolutions.getBovines()));
		Iterator<Bovine> i = bovines.iterator();
		while(i.hasNext())
		{
			Bovine b = i.next();
			if(b.getOffspringAverageSellPrice() == Double.NEGATIVE_INFINITY)
			{
				i.remove();
			}
		}
		Collections.sort(bovines, Comparators.bovineOffspringAveSellPrice);
		returnPanel.setEmptyMessage("None to show at this time");
		for(Bovine b: bovines)
		{
			returnPanel.addRow(new JLabel(b.getEartag().getEartagID()), new JLabel(String.valueOf(b.getOffspring().length)),
					new JLabel(String.valueOf(b.getNoOffspringSold())), new JLabel(String.valueOf(b.getNumOffspringNotSold())),
					new JLabel(String.valueOf(b.getOffspringAverageSellPrice())));
			
			final long id = b.getID();
			returnPanel.addMouseListener(new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(MainFrame.getInstance().showBovine(id)) {
						//show cattle not found message?
					}
				}
				@Override public void mouseEntered(MouseEvent arg0) {}
				@Override public void mouseExited(MouseEvent arg0) {}
				@Override public void mousePressed(MouseEvent arg0) {}
				@Override public void mouseReleased(MouseEvent arg0) {}
			});
		}
		
		return returnPanel;
	}
	
	/**
	 * Calculate worst sellers.
	 * @return Table of worst sellers.
	 */
	public Table getWorstSellers()
	{
		Table returnPanel = new Table("Top 30 Worst Sellers", true, "Visual Tag", "No. of Offspring", "Offspring sold", 
				"Offspring not yet sold", "Average Sell Price");
		returnPanel.setHeaderClickable(0, false);
		returnPanel.setHeaderClickable(1, false);
		returnPanel.setHeaderClickable(2, false);
		returnPanel.setHeaderClickable(3, false);
		returnPanel.setHeaderClickable(4, false);
				
		ArrayList<Bovine> bovines = new ArrayList<Bovine>(Arrays.asList(LivestockSolutions.getBovines()));
		Iterator<Bovine> i = bovines.iterator();
		while(i.hasNext())
		{
			Bovine b = i.next();
			if(b.getOffspringAverageSellPrice() == Double.NEGATIVE_INFINITY)
			{
				i.remove();
			}
		}
		Collections.sort(bovines, Collections.reverseOrder(Comparators.bovineOffspringAveSellPrice));
		returnPanel.setEmptyMessage("None to show at this time");
		for(Bovine b: bovines)
		{
			returnPanel.addRow(new JLabel(b.getEartag().getEartagID()), new JLabel(String.valueOf(b.getOffspring().length)),
					new JLabel(String.valueOf(b.getNoOffspringSold())), new JLabel(String.valueOf(b.getNumOffspringNotSold())),
					new JLabel(String.valueOf(b.getOffspringAverageSellPrice())));
			
			final long id = b.getID();
			returnPanel.addMouseListener(new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(MainFrame.getInstance().showBovine(id)) {
						//show cattle not found message?
					}
				}
				@Override public void mouseEntered(MouseEvent arg0) {}
				@Override public void mouseExited(MouseEvent arg0) {}
				@Override public void mousePressed(MouseEvent arg0) {}
				@Override public void mouseReleased(MouseEvent arg0) {}
			});
		}
		
		return returnPanel;
	}

}
